package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;

    /**
     * 角色管理-新增角色
     *
     * @param map
     * @return
     */
    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result save(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = (JSONObject) map.get("role");
        Role role = jsonObject.toJavaObject(Role.class);

        JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
        Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});

        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        Integer[] menuIds = jsonArray2.toArray(new Integer[]{});

        try {
            roleService.save(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 角色管理-分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    /**
     * 角色管理-编辑回显基本信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 编辑回显被关联的权限
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPermissionIdsById(Integer id) {
        try {
            Integer[] permissionIds = roleService.findPermissionIdsById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 编辑回显被关联的菜单
     * @param id
     * @return
     */
    @RequestMapping("/findMenuIdsById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findMenuIdsById(Integer id) {
        try {
            Integer[] menuIds = roleService.findMenuIdsById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 编辑角色
     * @param map
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = (JSONObject) map.get("role");
        Role role = jsonObject.toJavaObject(Role.class);

        JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
        Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});

        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        Integer[] menuIds = jsonArray2.toArray(new Integer[]{});

        try {
            roleService.edit(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delById(Integer id) {
        try {
            roleService.delById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (RuntimeException e){
          e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAll() {
        try {
            List<Role> roleList=roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
